<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Start of file administrator.php */
/* Location: ./auth/controllers/administrator.php */
class Administrator extends CI_Controller {

	function __construct(){
		parent::__construct(); /* function construct will determine all function */
		$this->load->model('auths'); /* Load Model from auth/models/auths */
    }
   
	public function index(){
		//$SERVER      = '120.89.88.14';
		//$PORT        =  389;
		//echo $ds = ldap_connect($SERVER,$PORT);
		$this->ws->set('title',lang('login')); /** page title **/
        $this->ws->set_theme('smooth-login'); /** set theme to smooth login */
		$this->ws->view('index'); /** view layout index, to views/index*/
	}
	
	function do_login(){
	    /** check if username and password empty **/   
		if(($this->input->post('username')== "") && ($this->input->post('password') == "")):
			$message = "Fill up Username and  Password";
			$bg_color = "#FFEBE8";
			$status   = "failed";
			$redirect = '';
		/** check if username not empty and password empty **/
        elseif(($this->input->post('username')!= "") && ($this->input->post('password') == "")):
			$message = "Fill up Password";
			$bg_color = "#FFEBE8";
			$status   = "failed";
			$redirect = '';
        /** check if username empty and password not empty **/
		elseif(($this->input->post('username')== "") && ($this->input->post('password') != "")):
			$message = "Fill up Username";
			$bg_color = "#FFEBE8";
			$status   = "failed";
			$redirect = '';	
		/** check if username not empty and password  not empty **/
        elseif (($this->input->post('username') != "") || ($this->input->post('password') != "")):
			/** LDAP Username And Password if verified **/
			$DEBUG       = 0;
			$GRACEPERIOD = 10 * 60;               // 10 minutes in seconds
			$SEARCHBASE  = 'ou=Users,dc=bbgemilang,dc=co,dc=id';
			$SERVER      = '120.89.88.14';
			$PORT        =  389;
			$username    = $this->input->post('username');
			$password    = $this->input->post('password');
			$ds = ldap_connect($SERVER,$PORT);
			
			$filter = '(uid=' . $username . ')';
			$sr = @ldap_search($ds,$SEARCHBASE,$filter,array('uid'));
			$info = ldap_get_entries($ds,$sr);
			$userdn = $info[0]['dn'];
			$r = @ldap_bind($ds,$userdn,$password);
			if($r):
				$value 		=	$this->auths->getUserData($this->input->post('username'));
				$user_data 	=	array('username'=>$value['user'],'dep'=>$value['Department']);
				$this->session->set_userdata($user_data);
				/** status user verification successfull **/
				$message  = "Username and password matched.";
				$bg_color = "#FFA";
				$status   = "success";
				$redirect = site_url('dashboard/administrator',301);
			else:
				$message  = "Username or password do not match.";
				$bg_color = "#FFEBE8";
				$status   = "failed";
				$redirect = '';
			endif;
		endif;
        /** JSON output **/
		$output = '{"message": "'.$message.'","status": "'.$status.'","redirect": "'.$redirect.'","bg_color": "'.$bg_color.'"}';
		echo $output;
	}
	
    /* function log out*/
    function do_logout(){
        $this->session->sess_destroy();
		redirect('/',301);
    }

}
/* End of file administrator.php */
/* Location: ./auth/controllers/administrator.php */